#########################################################################
# File : /lua/system/callback.lua
# Description : Callback implementation
# Author : GPG
#########################################################################

--[[
	Call Callback:Add(fun) to set a trigger function which is called when the 
	event is set. Arguments passed to the trigger function are the same as those 
	passed to Call(...)
	You can add as many callbacks as you want.
--]]
Callback = Class {
    __init = function(self)
        self.EventCallbacks = {}
        self.EventIsSet = false
        self.ManualReset = false
    end,

    #
    # Add a function to be called when this event is set. If the event is already set, 
    # nothing happens.
    #
    Add = function(self, fn, cbObj)
        if not fn then
            return
        end
        table.insert(self.EventCallbacks, {Func = fn, Caller = cbObj})
    end,
    
    #
    # Remove a callback function.
    #
    Remove = function(self,fn)
        for k,cb in self.EventCallbacks do
            if cb.Func == fn then
                table.remove(self.EventCallbacks,k)
            end
        end
    end,

    #
    # Set the event, calling all triggers waiting on it
    #
    Call = function( self, ... )
        if not self.EventIsSet then
            self.EventIsSet = true

            for k, cb in self.EventCallbacks do
                if cb.Caller then
                    cb.Func(cb.Caller, unpack(arg))
                else
                    cb.Func(unpack(arg))
                end
            end
        end
        
        if not self.ManualReset then
            self.EventIsSet = false
        end
    end,

    #
    # Reset the event
    #
    Reset = function(self)
        #LOG('*DEBUG: ..EventReset..')
        self.EventIsSet = false
    end,
    
    #
    # Clear all callbacks
    #
    Clear = function(self)
        self.EventCallbacks = {}
    end,

    #
    # Destroy the event. Any triggers waiting for it are abandoned.
    # We define 'Destroy' so we can stick this in a trashbag.
    #
    Destroy = function(self)
        self:ClearCallbacks()
    end,
}